﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.UI;
using YJKRevitWndr.InsertDoorAndWindow.InsertOperation;
using YArchitech.Revit;
using YRevitJig;

namespace YJKRevitWndr.InsertDoorAndWindow.Forms
{
	internal class XInputDialog : XUiJigBase, IDisposable
	{
		public XInputDialog(UIDocument uiDoc, InsertDoorAndWinForm userForm) : base(uiDoc)
		{
			base.OnMouseActivity += this.FloatingWindow;
			this.OnKeyDownEvent = (KeyEventHandler)Delegate.Combine(this.OnKeyDownEvent, new KeyEventHandler(this.OnKeyDown));
			this.OnMouseLeaveFormClientEvent = (WndMsgManager.MouseLeaveFormClientEvnetHandler)Delegate.Combine(this.OnMouseLeaveFormClientEvent, new WndMsgManager.MouseLeaveFormClientEvnetHandler(this.OnMouseLeaveFormClientEvnetHandler));
			this.OnMouseMoveIntoFormClientEvent = (WndMsgManager.MouseMoveIntoFormClientEvnetHandler)Delegate.Combine(this.OnMouseMoveIntoFormClientEvent, new WndMsgManager.MouseMoveIntoFormClientEvnetHandler(this.OnMouseMoveIntoFormClientEvnetHandler));
			this.m_UserForm = userForm;
		}

		private void OnKeyDown(object sender, KeyEventArgs e)
		{
			if (this.inputForm != null && this.inputForm.Visible)
			{
				this.inputForm.OnKeyDown(sender, e);
			}
		}

		private void OnMouseLeaveFormClientEvnetHandler(object sender, EventArgs e)
		{
			System.Windows.Forms.Form userForm = this.m_UserForm;
		}

		private void OnMouseMoveIntoFormClientEvnetHandler(object sender, EventArgs e)
		{
			if (this.m_UserForm != null)
			{
				InsertDoorAndWindowManage.m_dis();
			}
		}

		private void FloatingWindow(object sender, MouseEventArgs mouseEvent)
		{
			this.m_ptCur = mouseEvent.Location;
			if (this.inputForm != null)
			{
				Size size = new Size(2, 2);
				System.Drawing.Point ptCur = this.m_ptCur;
				JigFuncs.Client2Screen(base.JigForm.Handle, ref ptCur);
				int num = 0;
				int num2 = 0;
				int num3 = 0;
				int num4 = 0;
				RevitViewExtension.GetRevitWndRectangle(base.UiViewActive, ref num, ref num2, ref num3, ref num4);
				if (num >= ptCur.X || num3 <= ptCur.X || num2 >= ptCur.Y || num4 <= ptCur.Y)
				{
					this.inputForm.DlgVisible(false);
				}
				else if (num3 - ptCur.X < this.inputForm.Width + size.Width)
				{
					int num5 = this.inputForm.Width + size.Width - (num3 - ptCur.X);
					this.inputForm.Location = new System.Drawing.Point(ptCur.X + size.Width - num5, ptCur.Y + size.Height);
					this.inputForm.DlgVisible(true);
				}
				else
				{
					this.inputForm.Location = new System.Drawing.Point(ptCur.X + size.Width, ptCur.Y + size.Height);
					this.inputForm.DlgVisible(true);
				}
				if (this.inputForm.Visible && this.inputForm.MouseMoveArgs != null)
				{
					this.inputForm.MouseMoveArgs();
				}
			}
		}

		public void SetPromptStateNew(string strPrompt, bool bShowPrompt, string strInput, bool bShowInput)
		{
			if (bShowPrompt || bShowInput)
			{
				if (this.inputForm == null)
				{
					System.Drawing.Color viewBackgroundReverseColor = RevitVersionFuncs.GetViewBackgroundReverseColor(base.UiDoc.Application.Application);
					this.inputForm = new InputForm(this.m_hMDIActiveChildClientWnd, viewBackgroundReverseColor);
					this.inputForm.Opacity = 0.5;
				}
				this.inputForm.SetDlgState(strPrompt, bShowPrompt, strInput, bShowInput);
				return;
			}
			if (this.inputForm != null)
			{
				this.inputForm.Dispose();
				this.inputForm = null;
			}
		}

		public new void Dispose()
		{
			base.Dispose();
			base.OnMouseActivity -= this.FloatingWindow;
			this.OnKeyDownEvent = (KeyEventHandler)Delegate.Remove(this.OnKeyDownEvent, new KeyEventHandler(this.OnKeyDown));
			if (this.inputForm != null)
			{
				this.inputForm.Dispose();
				this.inputForm = null;
			}
			this.OnMouseLeaveFormClientEvent = (WndMsgManager.MouseLeaveFormClientEvnetHandler)Delegate.Remove(this.OnMouseLeaveFormClientEvent, new WndMsgManager.MouseLeaveFormClientEvnetHandler(this.OnMouseLeaveFormClientEvnetHandler));
			this.OnMouseMoveIntoFormClientEvent = (WndMsgManager.MouseMoveIntoFormClientEvnetHandler)Delegate.Remove(this.OnMouseMoveIntoFormClientEvent, new WndMsgManager.MouseMoveIntoFormClientEvnetHandler(this.OnMouseMoveIntoFormClientEvnetHandler));
		}

		public InputForm inputForm;
	}
}
